package com.callbell.cas.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.callbell.cas.util.Encodes;
import com.callbell.cas.util.MD5Util;
import com.callbell.cas.util.ResourceBundleUtil;

/**
 * 第三方微信登录
 * @author xlh
 *
 */
public class ThirdWeChatLoginController extends AbstractController{
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String service = ServletRequestUtils.getStringParameter(request, "service", "");
		//String state = MD5Util.toMD5(Encodes.randomNumber());//请求和回调状态
		String state = "fgjh654fgjh654";//请求和回调状态
		if("ocl".equals(service)){//如果是ocl-server系统，这设置state为：fgjh654fgjh654
			state = "fgjh654fgjh654";
		}

		String appId = ResourceBundleUtil.getProperty("wechat.login.appid");//应用唯一标识
		String prefixUrl = ResourceBundleUtil.getProperty("wechat.login.prefixUrl");//微信生成登录二维码地址前缀
		String callbackUrl = Encodes.urlEncode(ResourceBundleUtil.getProperty("thirdLogin.redirect.url"));//授权成功后回调的应用地址
		String responseType = "code";
		String scope = "snsapi_login";
		
		request.getSession().setAttribute("loginState", state);
		request.getSession().setAttribute("service", service);
		String url = prefixUrl + "?" + "appid=" + appId + "&redirect_uri=" + callbackUrl + "&response_type=" + responseType + "&scope=" + scope + "&state=" + state + "#wechat_redirect";
		response.setCharacterEncoding("utf-8");
		response.sendRedirect(url);
		return null;
	}
}
